<template>
  <div class="app-container">
    <el-form class="search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.names" placeholder="请输入名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button size="small" icon="Refresh" @click="resetQuery">重置</el-button>

        <el-button size="small" type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['safety:label:remove']">删除</el-button>
      </el-form-item>

    </el-form>

    <el-table :height="height + 'px'" v-loading="loading" :data="positionList" border stripe
      @selection-change="handleSelectionChange" @row-click="clickRow">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="POI点名称" sortable align="center" prop="names" />
      <el-table-column label="类型" sortable align="center" prop="types" width="180">
        <template #default="scope">
          <dict-tag :options="safety_label_types" :value="scope.row.types" />
        </template>
      </el-table-column>
      <el-table-column label="联系人" sortable align="center" prop="people" />
      <el-table-column label="联系人电话" sortable align="center" prop="phone" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" label-position="right" label-width="100px" :model="form">
        <el-form-item label="部门" prop="deptId">
          <el-tree-select v-model="form.deptId" :data="deptOptions"
            :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择部门"
            check-strictly clearable filterable />
        </el-form-item>
        <el-form-item label="兴趣点名称" prop="names">
          <el-input v-model="form.names" placeholder="请输入兴趣点名称" />
        </el-form-item>
        <el-form-item label="类型" prop="types">
          <el-select v-model="form.types">
            <el-option v-for="dict in safety_label_types" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="people">
          <el-input v-model="form.people" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listLabel, getLabel, updateLabel, delLabel } from '@/api/safety/label.js'
import useSatetyMapStore from '@/store/modules/safetyMap'
const safeMapStore = useSatetyMapStore();
const { proxy } = getCurrentInstance();
const { safety_label_types } = proxy.useDict('safety_label_types');
const positionList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const title = ref("");
const single = ref(true);
const multiple = ref(true);
import { deptTreeSelect } from "@/api/system/user";
const deptOptions = ref([])
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
getDeptTree();
const data = reactive({
  form: {},
  queryParams: {
    names: ''
  }
});
const height = computed(() => {
  return safeMapStore.tabTableHeight
})
const { queryParams, form } = toRefs(data);

/** 查询实时位置列表 */
function getList(cache = false) {
  loading.value = true;
  if (cache) {
    queryParams.value.names = "";
  }
  listLabel(queryParams.value).then(response => {
    positionList.value = response.data;
    if (queryParams.value.names == '') {
      safeMapStore.setAllPois(JSON.parse(JSON.stringify(response.data)))
    }
    loading.value = false;
  });
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    lat: null,
    lon: null,
    name: null,
    type: '1',
    contact: null,
    phone: null,
  };
  proxy.resetForm("formRef");
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}



/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getLabel(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改POI";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateLabel(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList(true);
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delLabel(_ids);
  }).then(() => {
    loading.value = true;
    getList(true);
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}
// getList();
function clickRow(row, column, event) {
  safeMapStore.setMapCenterLngLat(row.lon, row.lat)
}
onMounted(() => {
  console.log('labes onMounted')
})
onUnmounted(() => {
  console.log('labes onUnmounted')
})
onActivated(() => {
  // 调用时机为首次挂载
  // 以及每次从缓存中被重新插入时
  getList();
  console.log('labes activeted')
})

onDeactivated(() => {
  // 在从 DOM 上移除、进入缓存
  // 以及组件卸载时调用
  console.log('labes unactiveted')
})
</script>
<style lang="scss" scoped>
.app-container {
  padding: 10px;

  .search-form {
    :deep(.el-form-item__label) {
      font-size: 12px;
    }

    :deep(.el-input__wrapper) {
      background-color: #11224B;
      box-shadow: 0 0 0 1px #4E5A75;
      padding: 0px 11px
    }

    :deep(.el-form-item--default) {
      margin-bottom: 5px;
    }

    :deep(.el-input__inner) {
      color: #fff;
      font-size: 12px;
      height: 24px;
      line-height: 24px;
    }
  }
}

:deep(.el-table) {
  background-color: #091940;
  color: #FFFFFF;
}

:deep(.el-table__header th) {
  background-color: #1C2F61 !important;
  color: #FFFFFF;
  border-color: #4E5A75;
}

:deep(.el-table .el-table__cell) {
  border-right: 1px solid #4E5A75;
  border-bottom: 1px solid #4E5A75;
}
:deep(.el-table th.el-table__cell.is-leaf){
  border-bottom: 1px solid #4E5A75;
}

:deep(.el-table.is-scrolling-left.el-table--border .el-table-fixed-column--left.is-last-column.el-table__cell) {
  border-right: 1px solid #4E5A75;
}

:deep(.el-table__body tr.el-table__row > td) {
  background-color: #091940;
}

:deep(.el-table__body tr.el-table__row--striped > td),
:deep(.el-table--striped .el-table__body tr.el-table__row--striped td.el-table__cell) {
  background-color: #0E204D;
}

:deep(.el-table__body tr.current-row >td),
:deep(.el-table--striped .el-table__body tr.el-table__row--striped.current-row td.el-table__cell) {
  background-color: #1C2F61
}

:deep(.el-table__body tr.hover-row.current-row > td),
:deep(.el-table__body tr.hover-row.el-table__row--striped.current-row > td),
:deep(.el-table__body tr.hover-row.el-table__row--striped > td),
:deep(.el-table__body tr.hover-row > td),
:deep(.el-table--enable-row-hover .el-table__body tr:hover>td.el-table__cell) {
  background-color: #1C2F61;
}</style>